home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12572 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  73 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.athene.co.uk!not-for-mail
  3. From: "C.J. Scaife" <JOLTSWIFT@Athene.co.uk>
  4. Subject: Re: Object constuction in function call.
  5. Message-ID: <31503E74.7C7B@Athene.co.uk>
  6. Date: Wed, 20 Mar 1996 17:20:52 +0000
  7. References: <4icmqh$8g6@insosf1.netins.net> <314C4D23.1A40@image.dk>
  8. Organization: JoltSwift Ltd.
  9. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  10. MIME-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13.  
  14. Poul Thomas Lomholt wrote:
  15. > Harold Howe wrote:
  16. > >
  17. > > Greetings.
  18. > >
  19. > > I need help.  If an object is contructed in a function call, how long does it
  20. > > exist.  For example, (this is not what I am doing, but it illustrates the
  21. > > point).
  22. > >
  23. > > class TCorners
  24. > >   {
  25. > >   public:
  26. > >     int w,x,y,z;
  27. > >     TCorners( int a, int b, int c, int d)
  28. > >       { w=a;   x=b; y=c;  z=d;}
  29. > >   }
  30. > >
  31. > > void display_coordinates(TCorners corners)
  32. > >   {
  33. > >   cout << "corners are" << corners.w << corners.x
  34. > >                         << corners.y << corners.z ;
  35. > >   }
  36. > >
  37. > > int main(void)
  38. > >   {
  39. > >   display_coordinates(TCorners(1,1,20,10));
  40. > >   display_coordinates(TCorners(1,5,25,15));
  41. > >   display_coordinates(TCorners(2,5,20,10));
  42. > >   }
  43. > >
  44. > > How many objects exist at the programs end?  Are the objects created for the
  45. > > function call, then nuked afterwards?  I think they hang around, in fact I
  46. > > know they hang around because if I call the display guy about 100 times the
  47. > > program finally blows up.  I would like to use this syntax if possible in the
  48. > > real program that I am writing.
  49. > >
  50. > > Any suggestions would be appreciated.
  51. > > Harold Howe
  52. > > hhowe@trgnet.com
  53. > The objects are created temporarily be sure about that, and
  54. > hence does not exist, when the program terminates. However it
  55. > would be a good idea to declare the display_coordinates to take
  56. > a "const TCorners&" instead of an object.
  57. > Regards from
  58. > Poul Thomas Lomholt
  59.  
  60. The temporary annonymous variables passed as parameters exist only for 
  61. the duration of the statement in which they are constructed. That is 
  62. exactly what you want here because it means they exist until the 
  63. function (to which they are passed) returns.
  64.  
  65. Beware of returning references to such a temporary object (for an 
  66. example see http://www.Athene.co.uk/Joltswift/csact003.htm).
  67.  
  68. C. Scaife
  69.  
  70.